package com.sfzd5.UI;

import com.google.gson.Gson;
import com.sfzd5.aiclassroom.modle.ExamQuestionsInfo;
import com.sfzd5.aiclassroom.modle.Question;
import org.apache.commons.io.FileUtils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

public class EditExamQuestionsDialog extends JDialog {
    private JPanel contentPane;
    private JButton buttonCancel;
    private JPanel mPanel;
    private JButton 编辑Button;
    private JButton 保存Button;
    private JScrollPane scrollP;
    private JPanel question_panel;

    Question curSelectedQuestion = null;
    java.util.List<Question> qList = new ArrayList<>();
    ExamQuestionsInfo info;

    public EditExamQuestionsDialog(ExamQuestionsInfo info) {
        setContentPane(contentPane);
        setModal(true);

        buttonCancel.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onCancel();
            }
        });

        // call onCancel() when cross is clicked
        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                onCancel();
            }
        });

        // call onCancel() on ESCAPE
        contentPane.registerKeyboardAction(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onCancel();
            }
        }, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);

        this.info = info;

        VerticalFlowLayout vfl = new VerticalFlowLayout();
        vfl.setVGap(10);
        question_panel.setLayout(vfl);

        编辑Button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (curSelectedQuestion == null)
                    return;
                QuestionAnswerEditDialog questionAnswerEditDialog = new QuestionAnswerEditDialog(curSelectedQuestion);
                Dimension d = new Dimension(800, 600);
                questionAnswerEditDialog.setSize(d);
                questionAnswerEditDialog.setTitle("编辑习题答案：" + curSelectedQuestion.id);
                questionAnswerEditDialog.setPreferredSize(d);
                questionAnswerEditDialog.setLocation(0, 0);
                questionAnswerEditDialog.pack();
                questionAnswerEditDialog.setVisible(true);

                /*
                if(questionAnswerEditDialog.ok){
                    curSelectedQuestion.answer = questionAnswerEditDialog.question.answer;
                }
                //传递的是对象，内容自动更改了，不用再手动更改
                */
            }
        });

        for (String k : info.getSortedContentKeyList()) {
            for (Question q : info.content.get(k)) {
                qList.add(q);
                QuestionSelectPanel qsp = new QuestionSelectPanel(q, itemListener);
                qsp.cb.setSelected(false);
                question_panel.add(qsp.p);
            }
        }
        保存Button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                File taskFile = new File(HomeWorkManager.examDir, info.id + ".json");
                String json = new Gson().toJson(info);
                try {
                    FileUtils.writeStringToFile(taskFile, json, "utf-8");
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        });
    }

    QuestionSelected itemListener = new QuestionSelected() {
        @Override
        public void stateChange(boolean selected, Question question) {
            if (selected) {
                curSelectedQuestion = question;
            }
        }
    };

    private void onCancel() {
        // add your code here if necessary
        dispose();
    }

    {
// GUI initializer generated by IntelliJ IDEA GUI Designer
// >>> IMPORTANT!! <<<
// DO NOT EDIT OR ADD ANY CODE HERE!
        $$$setupUI$$$();
    }

    /**
     * Method generated by IntelliJ IDEA GUI Designer
     * >>> IMPORTANT!! <<<
     * DO NOT edit this method OR call it in your code!
     *
     * @noinspection ALL
     */
    private void $$$setupUI$$$() {
        contentPane = new JPanel();
        contentPane.setLayout(new com.intellij.uiDesigner.core.GridLayoutManager(1, 1, new Insets(10, 10, 10, 10), -1, -1));
        final JPanel panel1 = new JPanel();
        panel1.setLayout(new BorderLayout(0, 0));
        contentPane.add(panel1, new com.intellij.uiDesigner.core.GridConstraints(0, 0, 1, 1, com.intellij.uiDesigner.core.GridConstraints.ANCHOR_CENTER, com.intellij.uiDesigner.core.GridConstraints.FILL_BOTH, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_CAN_SHRINK | com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_CAN_GROW, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_CAN_SHRINK | com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false));
        mPanel = new JPanel();
        mPanel.setLayout(new BorderLayout(0, 0));
        panel1.add(mPanel, BorderLayout.CENTER);
        final JToolBar toolBar1 = new JToolBar();
        mPanel.add(toolBar1, BorderLayout.NORTH);
        编辑Button = new JButton();
        编辑Button.setText("编辑答案");
        toolBar1.add(编辑Button);
        保存Button = new JButton();
        保存Button.setText("保存");
        toolBar1.add(保存Button);
        buttonCancel = new JButton();
        buttonCancel.setText("关闭");
        toolBar1.add(buttonCancel);
        final JPanel panel2 = new JPanel();
        panel2.setLayout(new BorderLayout(0, 0));
        mPanel.add(panel2, BorderLayout.CENTER);
        scrollP = new JScrollPane();
        scrollP.setHorizontalScrollBarPolicy(30);
        scrollP.setVerticalScrollBarPolicy(22);
        panel2.add(scrollP, BorderLayout.CENTER);
        question_panel = new JPanel();
        question_panel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
        scrollP.setViewportView(question_panel);
    }

    /**
     * @noinspection ALL
     */
    public JComponent $$$getRootComponent$$$() {
        return contentPane;
    }
}
